package com.example.travelapplication.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.example.travelapplication.R;
import com.example.travelapplication.dao.AttDao;
import com.example.travelapplication.dao.FavorDao;
import com.example.travelapplication.dao.RsvDao;
import com.example.travelapplication.database.TravelDatabase;
import com.example.travelapplication.entity.Attraction;
import com.example.travelapplication.entity.Favor;
import com.example.travelapplication.entity.Reservation;

import java.util.ArrayList;
import java.util.List;

public class DetailActivity extends AppCompatActivity {

    private List<Attraction> list = new ArrayList<>();
    private Attraction attraction;
    private int id;
    private ImageView imageView;
    private TextView tv_title;
    private TextView tv_loc;
    private TextView tv_sco;
    private TextView tv_bed;
    private TextView tv_des;
    private TextView tv_price;
    private TextView tv_guide;
    private TextView tv_wifi;
    private Button bt_rsv;
    private TravelDatabase travelDatabase;
    private AttDao attDao;
    private FavorDao favorDao;
    private RsvDao rsvDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("");
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        init();

    }

    private void init(){
        travelDatabase = Room.databaseBuilder(DetailActivity.this, TravelDatabase.class, "travelDb")
                .allowMainThreadQueries()
                .build();
        attDao = travelDatabase.attDao();
        favorDao = travelDatabase.favorDao();
        rsvDao = travelDatabase.rsvDao();

        id = getIntent().getIntExtra("id", -1);
        attraction = attDao.getAttById(id);

        imageView = (ImageView) findViewById(R.id.iv_detail_pic);
        tv_title = (TextView)findViewById(R.id.tv_detail_title);
        tv_loc = (TextView)findViewById(R.id.tv_detail_loc);
        tv_sco = (TextView)findViewById(R.id.tv_detail_sco);
        tv_bed = (TextView)findViewById(R.id.tv_detail_bed);
        tv_des = (TextView)findViewById(R.id.tv_detail_des);
        tv_price = (TextView)findViewById(R.id.tv_detail_price);
        tv_guide = (TextView)findViewById(R.id.tv_detail_guide);
        tv_wifi = (TextView)findViewById(R.id.tv_detail_wifi);
        bt_rsv = (Button)findViewById(R.id.bt_detail_rsv);

        setImage(imageView,attraction.pic);
        tv_title.setText(attraction.title);
        tv_loc.setText(attraction.location);
        tv_sco.setText(String.valueOf(attraction.score));
        tv_bed.setText(attraction.bed + "bed");

        if(!attraction.guide){
            tv_guide.setText("no guide");
        }

        if(!attraction.wifi){
            tv_wifi.setText("no wifi");
        }

        tv_des.setText(attraction.description);
        tv_price.setText("￥" + attraction.price + "/天");

        bt_rsv.setOnClickListener(listener);
        if(rsvDao.getRsvByAttId(attraction.attId) != null){
            bt_rsv.setText("已预约");
            bt_rsv.setBackground(getDrawable(R.drawable.bg_btn_cancel));
        }
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(rsvDao.getRsvByAttId(attraction.attId) == null){
                rsvDao.add(new Reservation(attraction.attId));
                bt_rsv.setText("已预约");
                bt_rsv.setBackground(getDrawable(R.drawable.bg_btn_cancel));
                Toast.makeText(DetailActivity.this, "预约成功", Toast.LENGTH_SHORT).show();
            }else{
                rsvDao.delete(rsvDao.getRsvByAttId(attraction.attId));
                bt_rsv.setText("立即预约");
                bt_rsv.setBackground(getDrawable(R.drawable.bg_btn_rsv));
                Toast.makeText(DetailActivity.this, "取消预约", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_detail,menu);
        MenuItem menuItem = menu.findItem(R.id.menu_fav);
        if(favorDao.getFavorByAttId(attraction.attId)!=null){
            menuItem.setIcon(R.drawable.fav1);
        }

        menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(@NonNull MenuItem item) {
                if(favorDao.getFavorByAttId(attraction.attId)==null){
                    favorDao.add(new Favor(attraction.attId));
                    menuItem.setIcon(R.drawable.fav1);
                    Toast.makeText(DetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }else{
                    favorDao.delete(favorDao.getFavorByAttId(attraction.attId));
                    menuItem.setIcon(R.drawable.fav);
                    Toast.makeText(DetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    private void setImage(ImageView iv, String url) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.loading)
                .error(R.drawable.error)
                .centerCrop();
        Glide.with(DetailActivity.this)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(iv);
    }
}